package com.ruoyi.project.modules.interfaceinfo.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.modules.category.domain.Category;
import com.ruoyi.project.modules.category.service.ICategoryService;
import com.ruoyi.project.modules.domainInfo.domain.DomainObjectInfo;
import com.ruoyi.project.modules.domainInfo.service.IDomainInfoService;
import com.ruoyi.project.modules.errorCode.domain.ErrorCode;
import com.ruoyi.project.modules.errorCode.service.IErrorCodeService;
import com.ruoyi.project.modules.interfaceGenerator.service.IInterfaceGeneratorService;
import com.ruoyi.project.modules.interfaceinfo.domain.CategoryPageDto;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.domain.VirtualUsage;
import com.ruoyi.project.modules.interfaceinfo.service.IInterfaceInfoService;
import com.ruoyi.project.modules.interfaceinfo.service.IVirtualUsageService;
import com.ruoyi.project.modules.interfaceinfo.vo.InterfaceParam;
import com.ruoyi.project.modules.page.service.IPageService;
import com.ruoyi.project.modules.parameter.domain.InterfaceParamsFieldRelation;
import com.ruoyi.project.modules.parameter.domain.ParamsFieldRelation;
import com.ruoyi.project.modules.parameter.service.IInterfaceParamsFieldRelationService;
import com.ruoyi.project.modules.parameter.service.IParamsFieldRelationService;
import com.ruoyi.project.modules.processBranch.domain.ProcessBranch;
import com.ruoyi.project.modules.processBranch.service.IProcessBranchService;
import com.ruoyi.project.modules.processBranchCondition.domain.ProcessBranchCondition;
import com.ruoyi.project.modules.processBranchCondition.service.IProcessBranchConditionService;
import com.ruoyi.project.modules.processBranchConditionDetail.domain.ProcessBranchConditionDetail;
import com.ruoyi.project.modules.processBranchConditionDetail.service.IProcessBranchConditionDetailService;
import com.ruoyi.project.modules.processBranchEnd.domain.ProcessBranchEnd;
import com.ruoyi.project.modules.processBranchEnd.service.IProcessBranchEndService;
import com.ruoyi.project.modules.virtualUsageFieldMerge.domain.VirtualUsageFieldMerge;
import com.ruoyi.project.modules.virtualUsageFieldMerge.service.IVirtualUsageFieldMergeService;
import com.ruoyi.project.modules.virtualUsageGroup.domain.VirtualUsageGroup;
import com.ruoyi.project.modules.virtualUsageGroup.service.IVirtualUsageGroupService;
import com.ruoyi.project.modules.virtualUsageGroupDetail.service.IVirtualUsageGroupDetailService;
import com.ruoyi.project.modules.virtualUsageParamSource.domain.VirtualUsageParamSource;
import com.ruoyi.project.modules.virtualUsageParamSource.service.IVirtualUsageParamSourceService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author chenxiaoyu
 * @version 1.0
 * @date 2022/2/26 下午10:38
 */
@RestController
@RequestMapping("/interfaceGenerator")
@Api(tags = "接口信息")
public class InterfaceGeneratorController {

    @Resource
    private IInterfaceGeneratorService interfaceGeneratorService;

    @GetMapping("/method")
    @ApiOperation("method")
    public List<Map<String, Object>> method(@RequestParam(value = "interfaceName", required = false) String interfaceName
            , @RequestParam(value = "isEditTest", required = false) String isEditTest
            , @RequestParam(value = "isTimingTask", required = false) String isTimingTask
            , @RequestParam(value = "lifeCycle", required = false) String lifeCycle) {
        return interfaceGeneratorService.query(interfaceName, "1", isEditTest, isTimingTask, lifeCycle);
    }

    @GetMapping("/dubbo")
    @ApiOperation("dubbo")
    public List<Map<String, Object>> dubbo(@RequestParam(value = "interfaceName", required = false) String interfaceName
            , @RequestParam(value = "isEditTest", required = false) String isEditTest
            , @RequestParam(value = "isTimingTask", required = false) String isTimingTask
            , @RequestParam(value = "lifeCycle", required = false) String lifeCycle) {
        return interfaceGeneratorService.query(interfaceName, "3", isEditTest, isTimingTask, lifeCycle);
    }

    @GetMapping("/bff")
    @ApiOperation("bff")
    public List<Map<String, Object>> bff(@RequestParam(value = "interfaceName", required = false) String interfaceName
            , @RequestParam(value = "isEditTest", required = false) String isEditTest
            , @RequestParam(value = "isTimingTask", required = false) String isTimingTask
            , @RequestParam(value = "lifeCycle", required = false) String lifeCycle) {
        return interfaceGeneratorService.query(interfaceName, "2", isEditTest, isTimingTask, lifeCycle);
    }


    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IPageService pageService;

    @Autowired
    private IInterfaceInfoService interfaceInfoService;

    /**
     * 分类、页面、接口
     * @author chenxiaoyu
     * @date 2022/6/5 下午6:37
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/categoryPage")
    public AjaxResult categoryPage() {
        Category category = new Category();
        category.setSystemType("2");
        List<Category> categories = categoryService.selectCategoryList(category);
        Map<Long, List<Category>> map = categories.stream().collect(Collectors.groupingBy(Category::getParentId));
        List<CategoryPageDto> categoryPageDtoList = this.getCategoryPageDtoList(0L, map);
        return AjaxResult.success(categoryPageDtoList);
    }

    private List<CategoryPageDto> getCategoryPageDtoList(Long parentId, Map<Long, List<Category>> map) {
        List<Category> categoryList = map.get(parentId);
        List<CategoryPageDto> categoryPageDtoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(categoryList)) {
            for (Category c : categoryList) {
                CategoryPageDto dto = new CategoryPageDto();
                dto.setId(c.getId());
                dto.setName(c.getCategoryName());
                dto.setType("0");
                List<CategoryPageDto> children = getCategoryPageDtoList(dto.getId(), map);
                if (CollectionUtil.isEmpty(children)) {
                    dto.setType("1");
                    // 查询页面和接口
                    children = this.pageService.queryPageAndInterface(c.getId());
                }
                dto.setChildren(children);

                categoryPageDtoList.add(dto);
            }
        }
        return categoryPageDtoList;
    }

    @ApiOperation("更新接口状态（将 待生成代码 改为 生成代码待验证）")
    @GetMapping("/updateInterfaceStatus")
    public AjaxResult updateInterfaceStatus(Long interfaceId) {
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(interfaceId);
        if ("4".equals(interfaceInfo.getLifeCycle())) {
            interfaceInfo.setLifeCycle("5");
            this.interfaceInfoService.updateInterfaceInfo(interfaceInfo);
        }
        return AjaxResult.success();
    }
}
